<?php
/**
 * @title 1462. 课程表 IV
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

class Solution {

    /**
     * @param Integer $numCourses
     * @param Integer[][] $prerequisites
     * @param Integer[][] $queries
     * @return Boolean[]
     */
    function checkIfPrerequisite($numCourses, $prerequisites, $queries) {
        /**
         * @since 2024-01-29 先决条件，方向
         */
        $hash = [];
        foreach ($prerequisites as list($a, $b)){
            $hash[$a][$b] = true;
        }

        /**
         * @since 2024-01-29 课程编号从0到numCourses-1，逐一广度遍历
         */
        for($i=0; $i<$numCourses; $i++){
            /**
             * @since 2024-01-29 记录访问过的点，防止重复遍历
             */
            $visited = [$i=>true];

            /**
             * @since 2024-01-29 BFS广度遍历
             */
            $nodeArray = [$i=>true];
            while($nodeArray){
                /**
                 * @since 2024-01-29 下次遍历元素的集合
                 */
                $nextNodeArray = [];

                /**
                 * @since 2024-01-29 遍历当前元素集合
                 */
                foreach ($nodeArray as $node=>$val){

                    /**
                     * @since 2024-01-29 下次遍历的元素
                     */
                    if(isset($hash[$node])){
                        foreach ($hash[$node] as $nextNode=>$nextVal){
                            if(!isset($visited[$nextNode])){
                                $hash[$i][$nextNode] = true;
                                $visited[$nextNode] = true;
                                $nextNodeArray[$nextNode] = true;
                            } else {}
                        }
                    } else {}
                }

                /**
                 * @since 2024-01-29 下次遍历
                 */
                $nodeArray = $nextNodeArray;
            }
        }

        /**
         * @since 2024-01-29 查询数组，是否是先决条件
         */
        $result = [];
        foreach ($queries as list($a, $b)){
            if(isset($hash[$a][$b])){
                $result[] = true;
            } else {
                $result[] = false;
            }
        }

        /**
         * @return Boolean[]
         */
        return $result;
    }
}


/**
 * @url http://127.0.0.1/leetcode/202401/2024.01.29_4.php
 */
$datas = [
    [2, [[1,0]], [[0,1],[1,0]]],
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    /**
     * @since 2024-01-15 超时和错误案例参数
     */
    // $data = $xhprof->FormatArgs("/tmp/leetcode/xxx-xxx.txt");

    // $data[0] = $xhprof->listNode($data[0]);
    // $data[0] = $xhprof->tree($data[0]);
    echo $xhprof->DumpArgs($data);
    echo "\n<hr>\n";

    $obj = new Solution();
    $result = $obj->checkIfPrerequisite(...$data);
    echo $xhprof->DumpArgs([$result]);
    var_dump($result);

    echo "\n<hr>". str_repeat("<br>", 2) . str_repeat(PHP_EOL, 5);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
